/* eslint-disable vue/no-unused-vars */
<template>
  <div class="container">
    <!-- 卡片组件，内容写下内部 -->
    <el-card class="box-card" shadow="always">
      <!-- 头部区域 -->
      <div slot="header" class="clearfix">
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <el-form-item label="关键字" class="boxInput">
            <el-input v-model="formInline.user" placeholder="根据编号搜索"></el-input>
          </el-form-item>
          <el-form-item class="rightButton">
            <el-button class="btndey" @click="close">清空</el-button>
            <el-button type="primary" class="btndey" @click="onSubmit">搜索</el-button>
          </el-form-item>
        </el-form>
        <!-- 数据条数展示区域 -->
        <el-alert
          :title="`数据一共 ${count} 条`"
          type="info"
          :closable="false"
          show-icon
        >
        </el-alert>
      </div>
      <!-- 表格区域 -->
      <el-table
        :data="tableData"
        style="width: 100%"
        :default-sort = "{prop: 'addTime', order: 'descending'}"
      >
        <el-table-column
          prop="id"
          label="编号"
          width="220"
        >
        </el-table-column>
        <el-table-column
          label="题型"
          width="80"
        >
          <template slot-scope="scope">
            <span>{{ scope.row.questionType === '1' ? '单选' : scope.row.questionType === '2' ? '多选' : '简答' }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="题目编号"
          width="220"
        >
        <template slot-scope="scope">
            <el-link :underline="false" type="primary" v-for="(item, index) in scope.row.questionIDs" :key="index" @click="OnDiby(item.id)">{{ item.number }}<br/></el-link>
        </template>
        </el-table-column>
        <el-table-column
          prop="addTime"
          label="录入时间"
          width="180"
        >
        </el-table-column>
        <el-table-column
          prop="totalSeconds"
          label="答题时间(s)"
        >
        </el-table-column>
        <el-table-column
          prop="accuracyRate"
          label="正确率(%)"
        >
        </el-table-column>
        <el-table-column
          prop="userName"
          label="录入人"
        >
        </el-table-column>
        <el-table-column
          prop="address"
          label="操作"
          width="80"
        >
        <!-- 表格的操作选项区域 -->
          <template slot-scope="scope">
            <el-button type="danger" plain circle icon="el-icon-delete" @click="deleteClick(scope.row.id)"></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        background
        :page-size="20"
        layout=" prev, pager, next, sizes, jumper"
        :page-sizes="[20, 30, 50, 100]"
        :total="count"
        @size-change="ChangeSize"
        @current-change="ChangeCurrent"
        :hide-on-single-page="true"
      >
    </el-pagination>

    <!-- 点击弹出题目预览 -->
    <el-dialog
      title="题目预览"
      :visible.sync="dialogVisible"
      width="900"
    >
      <RandomsDialog v-if="dialogVisible" :questionBankId="questionBankId" @close="dialogVisible = false"></RandomsDialog>
    </el-dialog>
    </el-card>
  </div>
</template>

<script>
import { randoms, removeRandoms } from '@/api/hmmm/questions.js'
import RandomsDialog from '@/module-hmmm/components/randoms-dialog'
export default {
  data () {
    return {
      formInline: {
        user: '' // 搜索编号
      },
      tableData: [],
      count: 0,
      page: 1,
      pagesize: 20,
      keyword: '',
      dialogVisible: false,
      questionBankId: ''
    }
  },
  components: {
    RandomsDialog
  },
  created () {
    this.getRandoms()
  },
  methods: {
    onSubmit () {
      this.keyword = this.formInline.user
      this.getRandoms()
    },
    async getRandoms (count) {
      try {
        const { data } = await randoms({
          page: this.page,
          pagesize: count || this.pagesize,
          keyword: this.keyword
        })
        count = null
        if (this.tableData.length < 10) {
          this.tableData = [...this.tableData, ...data.items]
        }
        this.tableData = data.items
        this.count = data.counts
      } catch (err) {
        console.log(err)
      }
    },
    ChangeSize (val) {
      this.pagesize = val
      this.getRandoms()
    },
    ChangeCurrent (val) {
      this.page = val
      this.getRandoms()
    },
    close () {
      this.formInline.user = ''
    },
    deleteClick (id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await removeRandoms({ id })
        this.tableData = this.tableData.filter(item => item.id !== id)
        this.count--
        if (this.tableData.length < 10) {
          this.getRandoms(10)
        }
        this.$message({
          type: 'success',
          message: '删除成功'
        })
      })
    },
    OnDiby (id) {
      this.dialogVisible = true
      this.questionBankId = id
    }
  }
}
</script>

<style scoped lang='scss'>
::v-deep.container {
  padding: 0 10px;
  margin: 10px 0;
  .text {
    font-size: 14px;
  }

  .item {
    margin-bottom: 18px;
  }

  .clearfix:before,
  .clearfix:after {
    display: table;
    content: "";
  }
  .clearfix:after {
    clear: both;
  }

  .box-card {
    width: 100%;
    .el-card__body{
      padding-top: 0px;
    }
    .rightButton{
      float: right;
    }
    .el-pagination__jump{
      margin-left: 0px;
    }
    .el-form-item__label{
      width: 80px;
    }
    .el-button{
      padding: 9px 15px;
    }
    .el-button--primary{
      margin-right: -10px;
    }
    .is-circle{
      width: 32px;
      height: 32px;
      display: flex;
      align-items: center;
      justify-content: center;
    }
    .btndey{
      width: 56px;
      height: 32px;
    }
    .el-button--medium{
      font-size: 12px;
    }
    .boxInput {
      .el-input__inner{
        width: 240px;
        height: 30px;
        font-size: 13px;
      }
    }
  }
  .el-card__header{
    border: none;
  }

  .el-table th.el-table__cell{
    background-color: #FAFAFA;
  }

  .el-pagination {
    width: 100%;
    float: right;
    margin: 22px;
    display: flex;
    justify-content: flex-end;
  }

  .el-dialog__body{
    padding: 0;
  }
  .el-dialog__header {
    background: -webkit-gradient(linear,left top,right top,from(#1493fa),to(#01c6fa));
    background: linear-gradient(90deg,#1493fa,#01c6fa);
    background: #409eff;
    border-radius: 9px 9px 0 0;
  }
}
</style>
